package jwt.lab02;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.CellConstraints;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.io.OutputStream;
import java.io.PrintStream;


public class JavaMail extends PrintStream {
	private JTextArea textArea1;
	private JPanel panel1;
	private JButton saveAttachmentsButton;
	private JButton checkMailButton;
	private JButton sendMessageButton;
	private JButton sendMessageWithAttachmentButton;
	private JButton replyLastMessageButton;
	private JButton forwardLastMessageButton;

	public JavaMail(OutputStream out) {
		super(out, true);
		checkMailButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new SwingWorker() {
					protected Object doInBackground() throws Exception {
						ShowInbox.main(new String[0]);
						return null;
					}
				}.execute();
			}
		});
		saveAttachmentsButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new SwingWorker() {
					protected Object doInBackground() throws Exception {
						SaveAttachments.main(new String[0]);
						return null;
					}
				}.execute();
			}
		});
		sendMessageButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new SwingWorker() {
					protected Object doInBackground() throws Exception {
						SendMail.main(new String[0]);
						return null;
					}
				}.execute();
			}
		});
		sendMessageWithAttachmentButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new SwingWorker() {
					protected Object doInBackground() throws Exception {
						SendMailWithAttachment.main(new String[0]);
						return null;
					}
				}.execute();
			}
		});
		replyLastMessageButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new SwingWorker() {
					protected Object doInBackground() throws Exception {
						ReplyLastMessage.main(new String[0]);
						return null;
					}
				}.execute();
			}
		});
		forwardLastMessageButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new SwingWorker() {
					protected Object doInBackground() throws Exception {
						ForwardLastMessage.main(new String[0]);
						return null;
					}
				}.execute();
			}
		});


	}

	public JavaMail(OutputStream out, boolean autoFlush) {
		super(out, autoFlush);
	}

	public void println(String x) {
		textArea1.append(x + "\n");
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame("JavaMail");
		JavaMail javaMail = new JavaMail(System.out);
		frame.setContentPane(javaMail.panel1);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);

		System.setOut(javaMail);
	}

	{
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
		$$$setupUI$$$();
	}

	/**
	 * Method generated by IntelliJ IDEA GUI Designer
	 * >>> IMPORTANT!! <<<
	 * DO NOT edit this method OR call it in your code!
	 *
	 * @noinspection HardCodedStringLiteral
	 */
	private void $$$setupUI$$$() {
		panel1 = new JPanel();
		panel1.setLayout(new FormLayout("fill:max(d;4px):noGrow,left:4dlu:noGrow,fill:280px:grow", "center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:341px:grow"));
		checkMailButton = new JButton();
		checkMailButton.setHorizontalTextPosition(0);
		checkMailButton.setText("Check mail");
		CellConstraints cc = new CellConstraints();
		panel1.add(checkMailButton, cc.xy(1, 1));
		saveAttachmentsButton = new JButton();
		saveAttachmentsButton.setHorizontalTextPosition(0);
		saveAttachmentsButton.setText("Save attachments");
		panel1.add(saveAttachmentsButton, cc.xy(1, 3));
		sendMessageButton = new JButton();
		sendMessageButton.setHorizontalTextPosition(0);
		sendMessageButton.setText("Send message");
		panel1.add(sendMessageButton, cc.xy(1, 5));
		sendMessageWithAttachmentButton = new JButton();
		sendMessageWithAttachmentButton.setHorizontalTextPosition(0);
		sendMessageWithAttachmentButton.setText("Send message with attachment");
		panel1.add(sendMessageWithAttachmentButton, cc.xy(1, 7));
		replyLastMessageButton = new JButton();
		replyLastMessageButton.setText("Reply last message");
		panel1.add(replyLastMessageButton, cc.xy(1, 9));
		forwardLastMessageButton = new JButton();
		forwardLastMessageButton.setText("Forward last message");
		panel1.add(forwardLastMessageButton, cc.xy(1, 11));
		final JScrollPane scrollPane1 = new JScrollPane();
		panel1.add(scrollPane1, cc.xywh(3, 1, 1, 13, CellConstraints.FILL, CellConstraints.FILL));
		textArea1 = new JTextArea();
		textArea1.setBackground(new Color(-10452849));
		textArea1.setEditable(true);
		textArea1.setFont(new Font("Verdana", textArea1.getFont().getStyle(), textArea1.getFont().getSize()));
		textArea1.setForeground(new Color(-1));
		scrollPane1.setViewportView(textArea1);
	}

	public JComponent $$$getRootComponent$$$() {
		return panel1;
	}
}
